<template>
    <div class="app">
        <div class="chat-container">
            在线人数{{ onlineList.length }}
            <div class="chat-room">
                <div class="message-item" v-for="(item, index) in messageList" :key="index">
                    <div class="flex-left" v-if="item.userId === userInfo.userId">
                        <div class="avatar">
                            {{ "w" + item.userId }}
                        </div>
                        <div class="message-content" style="margin-left: 10px;">
                            {{ item.content }}
                        </div>
                    </div>
                    <div class="flex-right" v-else>
                        <div class="message-content" style="margin-right: 10px;">
                            {{ item.content }}
                        </div>
                        <div class="avatar">
                            {{ "w" + item.userId }}
                        </div>
                    </div>
                </div>
            </div>
            <div class="send-box">
                <input type="text" class="message-input" v-model="yourMessage" @keyup.enter="sendMessage">
                <button class="send-btn" @click="sendMessage">发送</button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const yourMessage = ref('')
const messageList = ref([])
const onlineList = ref([])
const userInfo = ref({
    userId: ''
})
let ws = null

const sendMessage = () => {
    if (yourMessage.value) {
        ws.send(
            JSON.stringify({
                type: 'message',
                userId: userInfo.value.userId,
                content: yourMessage.value
            })
        )

        yourMessage.value = ''
    }
}

const initWebSocket = () => {
    ws = new WebSocket('ws://localhost:8888')
    console.log(ws)

    ws.onopen = () => {
        console.log('websocket连接成功')
        ws.send(
            JSON.stringify({
                type: 'init',
                userId: userInfo.value.userId,
                content: '欢迎来到子凡的狂欢派对'
            })
        )
    }

    ws.onerror = () => {
        console.log('websocket连接失败')
    }

    ws.onmessage = (data) => {
        const message = JSON.parse(data.data)
        switch (message.type) {
            case 'tips':
                console.log(message.content)
                break
            case 'message':
                messageList.value.push(message)
                break
            case 'onlineList':
                onlineList.value = message.list
                break
            default:
                break
        }
    }

    ws.close = () => {
        console.log('websocket连接关闭')
    }
}

onMounted(() => {
    userInfo.value.userId = new Date().getTime().toString().slice(8)
    initWebSocket()
})
</script>

<style scoped>
.app {
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-container {
    width: 600px;
    height: 600px;
    overflow: hidden;
    background: #ccc;
    border-radius: 10px;
}

.chat-room {
    height: calc(100% - 100px);
    padding: 10px;
    box-sizing: border-box;
    overflow: auto;
    background: #dbc1c1;
}

.message-item {
    width: 80%;
    margin: 0 auto;
    margin-bottom: 10px;
}

.flex-left {
    display: flex;
    justify-content: flex-start;
}

.flex-right {
    display: flex;
    justify-content: flex-end;
}

.avatar {
    width: 40px;
    height: 40px;
    line-height: 40px;
    border-radius: 50%;
    overflow: hidden;
    background: #fafafa;
}

.message-content {
    min-height: 30px;
    background: #fff;
    border-radius: 8px;
    padding: 10px;
}

.send-box {
    height: 80px;
    display: flex;
    align-items: center;
}

.message-input {
    height: 60px;
    flex: 1;
}

.send-btn {
    width: 150px;
    height: 60px;
    line-height: 60px;
    text-align: center;
}
</style>